<template>
	<zh-form ref="dictForm" :model="state.form" style="margin-top: 20px">
		<zh-form-input prop="dictField" label="所属字段" placeholder="请输入所属字段" v-model="state.form.dictField"></zh-form-input>
		<zh-form-input prop="fieldName" label="字段名称" placeholder="请输入字段名称" v-model="state.form.fieldName"></zh-form-input>
		<zh-form-select prop="status" label="状态" placeholder="请选择" :options="state.statusDict" v-model="state.form.status"></zh-form-select>
		<zh-form-input prop="dictKey" label="字典属性" placeholder="请输入字段名称" v-model="state.form.dictKey"></zh-form-input>
		<zh-form-input prop="dictValue" label="字典属性名称" placeholder="请输入字段名称" v-model="state.form.dictValue"></zh-form-input>
		<zh-form-select prop="fixed" label="是否可编辑" placeholder="请选择" :options="state.dictData.flag" v-model="state.form.fixed"></zh-form-select>
		<zh-form-select
			prop="parentField"
			label="父字段"
			placeholder="请选择"
			:options="state.dictDataField.fieldData"
			:fieldNames="{
				label: 'fieldName',
				value: 'dictField',
				showLabel: ['dictField', 'fieldName'],
			}"
			filterable
			:filter-method="filterOption"
			@change="handleParentFieldChange"
			v-model="state.form.parentField"
		></zh-form-select>
		<zh-form-select
			prop="parentKey"
			label="父字段属性"
			placeholder="请选择"
			:options="state.dictDataField.keyData"
			:fieldNames="{
				label: 'dictValue',
				value: 'dictKey'
      }"
			filterable
			:filter-method="filterOption"
			v-model="state.form.parentKey"
		></zh-form-select>
		<zh-form-input prop="classify" label="所属分类" placeholder="" v-model="state.form.classify"></zh-form-input>
		<zh-form-item label="图标" prop="icon">
			<iconSelector placeholder="请输入菜单图标" v-model="state.form.icon" type="all" />
		</zh-form-item>
		<zh-form-input prop="orderNo" label="排序" placeholder="" v-model="state.form.orderNo"></zh-form-input>
		<zh-form-input prop="remarks" type="textarea" :span="24" label="备注" v-model="state.form.remarks"></zh-form-input>
	</zh-form>
</template>

<script lang="ts" setup name="DictForm">
//----------------定义接收参数-------------------------------
const props = defineProps({
	dictInfo: {
		type: Object,
		default: () => {},
		required: false,
	},
	modelValue: Boolean,
});
const emit = defineEmits(['update:modelValue']);

//------------------定义子组件---------------------------
const dictForm = ref<any>({});

//------------------内部变量定义-------------------------
const state = reactive({
	statusDict: [
		{
			dictKey: '0',
			dictValue: '禁用',
		},
		{
			dictKey: '1',
			dictValue: '启用',
		},
	],
	dictData: {} as any,
	orgSelectTreeData: [],
	form: {} as any,
	initData: {} as any,
	iconSelectTreeData: [],
	dictDataField: {} as any,
});

//------------------定义调用方法-------------------------
function handleParentFieldChange(value) {
	const params = {
		dictField: value,
	};
	$api.common.send.post('dict0001', params).then((res) => {
		state.dictDataField.keyData = res.result.list;
    console.log(res.result.list)
		state.form.parentKey = undefined;
    console.log(state.dictDataField.keyData);
	});
}
function filterOption(input) {
	if (typeof input !== 'undefined') {
		state.dictDataField.fieldData = state.dictDataField.allFieldData.filter(
			(item) => item.dictField.indexOf(input) > -1 || item.fieldName.indexOf(input) > -1
		);
	} else {
		state.dictDataField.fieldData = state.dictDataField.allFieldData;
	}
}
function saveDict() {
	// 表单验证
	return new Promise((resolve, inject) => {
		dictForm.value.validate((valid) => {
			if (valid) {
				const dict: any = state.form;
        console.log(dict)
				if (dict.sysId) {
					setTimeout(() => {
						$api.common.send
							.post('dict0005', dict)
							.then(() => {
								resolve(true);
								emit('update:modelValue', false);
								$messageBox.success('修改成功', '成功提示');
							})
							.catch(() => {
								inject();
							});
					}, 20);
				} else {
					setTimeout(() => {
						$api.common.send
							.post('dict0004', dict)
							.then(() => {
								$messageBox.success('新增成功', '成功提示');
								resolve(false);
								emit('update:modelValue', false);
							})
							.catch(() => {
								inject();
							});
					}, 20);
				}
			}
		});
	});
}

//------------------初始化------------------------------
// 数据字典初始化
let parentField: any = null;
if (props.dictInfo) {
	state.initData = Object.assign({}, props.dictInfo);
	state.form = state.initData;
	parentField = state.initData.parentField;
} else {
	state.initData = {};
}
// 数据字典初始化
const params = {
	dictField: parentField,
};
$api.common.send.post('dict0024', params).then((res) => {
	state.dictDataField = res.result;
});

const dictParams = {
	dictFields: 'flag',
};
$api.common.getBatchDict.post(dictParams).then((res) => {
	state.dictData = res.result;
});
defineExpose({ saveDict });
</script>
